package com.px.business.service.impl;

import com.px.business.domain.QnrAnswers;
import com.px.business.domain.dto.AnswersResultDTO;
import com.px.business.domain.dto.PaperAnswersDTO;
import com.px.business.domain.dto.QuestionsAnswersDTO;
import com.px.business.mapper.PaperAnswersMapper;
import com.px.business.service.IPaperAnswersService;
import com.px.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class PaperAnswersServiceImpl implements IPaperAnswersService {
    @Autowired
    private PaperAnswersMapper paperAnswersMapper;

    @Override
    public PaperAnswersDTO selectPaperAnswersList(Long evaluationApplicationId) {
        PaperAnswersDTO paperAnswers = paperAnswersMapper.selectPaperAnswersList(evaluationApplicationId);
        Map<Long, List<QnrAnswers>> answersGroupMap = paperAnswers.getQnrAnswersList().stream().collect(Collectors.groupingBy(QnrAnswers::getPaperQuestionsId));
        paperAnswers.getQnrQuestionsList().stream().peek(bean -> {
            // 赋值
            bean.setQnrAnswersList(answersGroupMap.get(bean.getPaperQuestionsId()));

            // 所有答过问题的数量
            bean.setQuestionsCount(bean.getQnrAnswersList().size());
            // 插入所有选项答过的人数、分数、占比
            List<AnswersResultDTO> answersResultList = new ArrayList<>();
            Map<Long, List<QnrAnswers>> answersMap = bean.getQnrAnswersList().stream().collect(Collectors.groupingBy(QnrAnswers::getAnswersValue));
            answersMap.forEach((k, v) -> {
                answersResultList.add(new AnswersResultDTO(k, v.size(), (double) v.size() / (double) bean.getQuestionsCount()));
                bean.setAnswersResultList(answersResultList);
            });
            if (StringUtils.isNotEmpty(bean.getAnswersResultList()) && bean.getAnswersResultList().size() > 0) {
                // 所有答过问题的平均分
                LongSummaryStatistics answersValue = bean.getAnswersResultList().stream().collect(Collectors.summarizingLong(AnswersResultDTO::getAnswersValue));
                IntSummaryStatistics answersCount = bean.getAnswersResultList().stream().collect(Collectors.summarizingInt(AnswersResultDTO::getAnswersCount));
                bean.setQuestionsAvgScore((double) answersValue.getSum() / (double) answersCount.getSum());
            }
        }).collect(Collectors.toList());
        // 问卷满分
        LongSummaryStatistics paperFullScore = paperAnswers.getQnrQuestionsList().stream().collect(Collectors.summarizingLong(QuestionsAnswersDTO::getFullScore));
        paperAnswers.setPaperFullScore(paperFullScore.getSum());
        // 问卷得分
        DoubleSummaryStatistics paperAvgScore = paperAnswers.getQnrQuestionsList().stream().filter(b -> b.getQuestionsAvgScore() != null)
                .collect(Collectors.summarizingDouble(QuestionsAnswersDTO::getQuestionsAvgScore));
        paperAnswers.setPaperAvgScore(paperAvgScore.getSum());
        return paperAnswers;
    }
}
